<template>
  <div id="comment">
    <i-input class="editComment" v-model="keyVal" placeholder="写评论..." icon="md-create"></i-input>
    <Button type="success" @click.native="tocomment">发表</Button>
    <span class="commentNum" @click.native="tocomment">{{commentNum}}</span>
    <Icon
      style="float:right"
      type="ios-share-alt"
      color="#d43d3d"
      size="30"
      class="commentInco fr"
      @click.native="showSharebox"
    ></Icon>
    <Icon
      style="float:right"
      type="ios-cloud-outline"
      @click.native="t"
      v-show="!collected"
      size="30"
      class="commentInco fr"
    ></Icon>
    <Icon
      style="float:right"
      type="ios-cloud"
      @click.native="t"
      v-show="collected"
      size="30"
      color="#d43d3d"
      class="commentInco fr"
    ></Icon>
  </div>
</template>
<script>
import * as type from "../store/mutation-types.js";
import { mapGetters } from "vuex";
import Vue from "vue";
Vue.component("my-commentNum", {
  template: `<span class="commentNum"></span>`
});
export default {
  props: ["comment"],
  data() {
    return {
      commentNum: this.comment > 0 ? this.comment : "0",
      collected: this.$route.params.collected,
      keyVal:"",
      replys: [],
    };
  },
  methods: {
    t() {
      this.collected = !this.collected;
    },
    showSharebox() {
      this.$store.commit(type.SHOWSHAREBOX, true);
    },
    tocomment() {
     if(this.logined){
      //主要是通过$emit方法来实现传参的方式，第一个参数是自定义事件名称，第二个则是要传的数据
      this.replys.push({
        "username":this.userName,
        "content":this.keyVal
      })
      this.$emit("getKeyVal",this.replys);
			}else{
				const _this = this;
				this.$confirm('登录后才能发表评论, 是否前往登录?', '提示', {
		          	confirmButtonText: '确定',
		          	cancelButtonText: '取消',
		          	type: 'warning'
		        })
		        .then(() => {
		          	_this.$router.push({path:'/my'});
		        })
		        .catch(() => { 
		        });
			}
    }
  },
  computed: {
    ...mapGetters(["logined","userName"])
  }
};
</script>
<style lang="scss">
#comment {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 1.3rem;
  z-index: 100;
  background: #fff;
  .editComment {
    display: inline-block;
    width: 54%;
    margin-left: 0.2rem;
    margin-top: 0.1rem;
    .ivu-input {
      height: 55px;
    }
    .ivu-input-icon{
      line-height: 55px;
    }
  }
  .ivu-btn {
    margin-left: 10px;
    width: 85px;
    height: 55px;
  }
  .commentInco {
    margin-top: 0.1rem;
    margin-right: 0.2rem;
  }
  .showcomment {
    position: relative;
  }
  .commentNum {
    position: absolute;
    padding: 0.05rem;
    border-radius: 80%;
    min-width: 0.55rem;
    text-align: center;
    line-height: 0.45rem;
    background: #d43d3d;
    right: 22%;
    top: 0.17rem;
    color: #fff;
    font-size: 10px;
  }
  button {
    margin-top: 5px;
  }
}
</style>